﻿
function FormulaParser(){
	//条件关键字常量
	this.IF_KEY = "如果";
	this.THEN_KEY = "则";
	this.ELSE_KEY = "否则";
	
	this.OR_KEY = "或";
	this.AND_KEY = "且";
	this.END_SPLIT = ";";
	
	//条件变量包含在[]里面
	this.Variable_Left_Key = "[";
	this.Variable_Right_Key = "]";
	
	//值变量包含在{}里面
	this.Value_Left_Key = "{";
	this.Value_Right_Key = "}";
	//条件不满足计算返回的结果
	this.returnValue = 0;
	
	this.expressionParser = new ExpressionParser();
}

FormulaParser.prototype = {
	parseFormula:function(formula,valueMap){
		var ifChIndex = formula.indexOf(this.IF_KEY);
		if(ifChIndex==-1){//没有条件的公式
			var formulaValue = "";
			var name = "";
			for(var i=0;i<formula.length;i++){
				var c = formula.charAt(i);
				if(c==this.Variable_Left_Key || c==this.Value_Left_Key){
					name = "";
					for(var j=i+1;j<formula.length;j++){//寻找对应的变量标志符号
						var c2 = formula.charAt(j);
						if(c2==this.Variable_Right_Key || c2==this.Value_Right_Key){// [ 或 { 为变量的开始
							var value = valueMap.get(name);
							if(value==null){
								value = name;
							}
							formulaValue += value;
							i = j;
							break;
						}else{
							name += c2;
						}
					}
				}
				else{
					formulaValue += c;
				}
			}
			formulaValue = formulaValue.replace("<>","!=");
			//this.exception.debug("formulaValue = " + formulaValue);
			return this.expressionParser.calculate(formulaValue);
		}else{//有条件的中文公式
			var formulaValue = "";
			if(formula.indexOf(this.END_SPLIT)==-1){
				var formulaArr = formula.split(this.ELSE_KEY);
				var ifFormulaArr = formulaArr[0].split(this.IF_KEY);
				for(var j=0;j<ifFormulaArr.length;j++){
					var ifFormula = ifFormulaArr[j];
					if(!CalculateUtil.isNull(ifFormula)){
						ifFormula = ifFormula.replace(this.THEN_KEY, ",");
						ifFormula = ifFormula.replace(this.OR_KEY, "||");
						ifFormula = ifFormula.replace(this.AND_KEY, "&&");
						formulaValue += "if(" + ifFormula + ");";
					}
				}
				if(formulaArr.length>1){
					formulaValue += formulaArr[1];
				}
			}else{
				formula = formula.replace("如果", "if(");
				formula = formula.replace("则", ",");
				formula = formula.replace(this.END_SPLIT, ");");
				formula = formula.replace("否则", "");
				formula = formula.replace(this.OR_KEY, "||");
				formula = formula.replace(this.AND_KEY, "&&");
				formulaValue = formula;
			}
			//this.exception.debug("有条件的表达式：" + formulaValue);
			return this.parseFormula(formulaValue, valueMap);
		}
	},
	//计算某个人的所有工资项目的金额
	calAllMoney:function(allMap,row,col,money){
		var personMap = allMap.get(row);
		var formulaMap = personMap.get("formula");// 列号->公式
		var nameMap = personMap.get("name");// 列号->工资项目的名称
		
		var calNames = new Array();
		var currentName = nameMap.get(col);
		//alert("currentName = " + currentName);
		calNames.push(currentName);
		personMap.put(currentName,money);//重新设置当前的工资项目
		
		var indexMap = personMap.get("indexMap");// 计算顺序->列号
		var indexArr = personMap.get("indexArr");//计算顺序
		indexArr = CalculateUtil.QuickSort(indexArr);//对计算顺序进行排序
		for(var i=0;i<indexArr.length;i++){
			var index = indexArr[i];
			var currentCol = indexMap.get(index);
			var name = nameMap.get(currentCol);
			var formula = formulaMap.get(currentCol);
			//alert("formula = " + formula);
			if(!CalculateUtil.isNull(formula) && name!=currentName && this.isCal(calNames,formula)){//不计算当前列和无关的列
				//this.exception.debug(name + "=" + formula);
				var money2 = this.parseFormula(formula,personMap);
				//alert(name + "=" + money2);
				calNames.push(name);
				if(CalculateUtil.isNull(money2)) money2= "0";
				CellWeb1.SetCellDouble(currentCol,row,0,money2);
				personMap.put(name,money2);
			}
		}
		allMap.put(row,personMap);
		return allMap;
	},
	isCal:function(calNames,formula){
		for(var i=0;i<calNames.length;i++){
			if(formula.indexOf(calNames[i])!=-1){
				//alert("true");
				return true;
			}
		}
		return false;
	}
}